//
//  FKMenuView.m
//  FKMeituan
//
//  Created by 云眼科技 on 16/7/14.
//  Copyright © 2016年 CloudEyes. All rights reserved.
//

#import "FKMenuView.h"

@implementation FKMenuItem{
    NSString *_title;
}

- (instancetype)initWithTitle:(NSString *)title{
    self = [super init];
    if (self) {
        _title = title;
        [self commonInit];
    }
    return self;
}
-(instancetype)initWithFrame:(CGRect)frame title:(nonnull NSString *)title{
    self = [super initWithFrame:frame];
    if (self) {
        _title = title;
        [self commonInit];
    }
    return self;
}
-(void)commonInit{
    self.contentMode = UIViewContentModeCenter;
    self.titleLabel.textAlignment = NSTextAlignmentCenter;
    self.titleLabel.font = [UIFont systemFontOfSize:self.frame.size.height/2 - 2];
    [self setTitleForClose];
}
-(void)setTitleForOpen{
    [self setTitle:[NSString stringWithFormat:@"%@▼", _title] forState:UIControlStateNormal];
    [self setTitleColor:[UIColor cyanColor] forState:UIControlStateNormal];
}
-(void)setTitleForClose{
    [self setTitle:[NSString stringWithFormat:@"%@▲", _title] forState:UIControlStateNormal];
    [self setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
}
@end

NSInteger const fk_btn_tag = 10086;

@interface FKMenuView ()
@property (nonatomic, strong) NSMutableArray <FKMenuItem *> *itemArray;
@property (nonatomic, strong) NSMutableArray <UIView *> *lineViewArray;
@end
@implementation FKMenuView{
    FKMenuItem *_lastItem;
}

- (instancetype)initWithFrame:(CGRect)frame itemsTitleArray:(nonnull NSArray<NSString *> *)titleArray{
    self = [super initWithFrame:frame];
    if (self) {
        _itemArray = [[NSMutableArray alloc] initWithCapacity:titleArray.count];
        _lineViewArray = [[NSMutableArray alloc] initWithCapacity:titleArray.count-1];
        [titleArray enumerateObjectsUsingBlock:^(NSString * _Nonnull title, NSUInteger index, BOOL * _Nonnull stop) {
            FKMenuItem *item = [[FKMenuItem alloc] initWithTitle:title];
            [item addTarget:self action:@selector(FKMenuItemPressed:) forControlEvents:UIControlEventTouchUpInside];
            item.tag = fk_btn_tag + index;
            [self addSubview:item];
            [_itemArray addObject:item];
        }];

        for (int i = 1; i < _itemArray.count; i+=1) {
            UIView *lineView = [[UIView alloc] init];
            lineView.backgroundColor = [UIColor blackColor];
            [self addSubview:lineView];
            [_lineViewArray addObject:lineView];
        }
    }
    return self;
}

-(void)layoutSubviews{
    [super layoutSubviews];
    CGFloat itemWidth = self.frame.size.width/self.itemArray.count;
    CGFloat itemHeight = self.frame.size.height;
    [_itemArray enumerateObjectsUsingBlock:^(FKMenuItem * _Nonnull item, NSUInteger index, BOOL * _Nonnull stop) {
        item.frame = CGRectMake(itemWidth * index, self.bounds.origin.y, itemWidth, itemHeight);
    }];

    [_lineViewArray enumerateObjectsUsingBlock:^(UIView * _Nonnull lineView, NSUInteger index, BOOL * _Nonnull stop) {
        lineView.frame = CGRectMake(itemWidth * (index+1), self.bounds.origin.y, 1, itemHeight);
        if (index == _lineViewArray.count - 1) {
            *stop = YES;
        }
    }];
}


-(void)FKMenuItemPressed:(FKMenuItem *)item{
    if (_lastItem == item){
        [self resetStateForItem:item];
        return;
    } 
    [self resetStateForItem:_lastItem];
    [self resetStateForItem:item];
}

-(void)resetStateForItem:(FKMenuItem *)item{
    item.selected = !item.selected;    
    if (item.selected) {
        [item setTitleForOpen];
        _lastItem = item;
        if (self.delegate && [self.delegate respondsToSelector:@selector(menuView:didSelectedItemAtIndex:)]) {
            [self.delegate menuView:self didSelectedItemAtIndex:item.tag - fk_btn_tag];
        }
    }else{
        [item setTitleForClose];
        _lastItem = nil;
    }
}



@end
